<?php
namespace App\Repositories;

use App\Models\Questions;
use Illuminate\Support\Facades\DB;
use PDOException;
use App\Models\CompanyJobsType;
use Illuminate\Support\Facades\Log;

class QuestionRepository
{
    use BaseRepository;
    
    private $model;
    
    public function __construct(Questions $questions)
    {
        $this->model = $questions;
    }
    
    
    public function getByItembank($itemBankId,$pageSize = 0)
    {
        if(is_array($itemBankId)){
            $query = $this->model->whereIn('itembank_id',$itemBankId);
        } else {
            $query = $this->model->where('itembank_id',$itemBankId);
        }
        
        if($pageSize) {
            return $query->orderBy('created_at','desc')->paginate($pageSize);
        }

        return $query->orderBy('created_at', 'desc')->get();
    }

    public function getByItembankandSkill($itemBankId, $jobsTypeId, $pageSize = 0)
    {
        if (is_array($itemBankId)) {
            $query = $this->model->whereIn('itembank_id', $itemBankId);
        } else {
            //$query = $this->model->where('itembank_id',$itemBankId)->where('job_id',$jobsTypeId);
            $query = $this->model->where('job_id', $jobsTypeId);
        }

        if ($pageSize) {
            return $query->orderBy('created_at', 'desc')->paginate($pageSize);
        }

        return $query->orderBy('created_at','desc')->get();
    }
    
    
    /**
     * 随机获取规定数量的问题
     * 
     * @param int $type  0 单选  1 多选  2 判断 3 论述
     * @param int $itemId 题库编号
     * @param int $limit 数量
     */
    public function getRand($type,$itemId,$limit)
    {
        return $this->model->where(['questions_type'=>$type,'itembank_id'=>$itemId])->orderByRaw('RAND()')->take($limit)->lists('id');   
    }
    
    /**
     * @param int $itemBankId
     * @return array
     */
    public function getNum($itemBankId)
    {
        $radio_num = $this->model->where(['itembank_id'=>$itemBankId,'questions_type'=>0])->count();
        
        $mulit_num = $this->model->where(['itembank_id'=>$itemBankId,'questions_type'=>1])->count();
        
        $judge_num = $this->model->where(['itembank_id'=>$itemBankId,'questions_type'=>2])->count();
        
        $discuz_num = $this->model->where(['itembank_id'=>$itemBankId,'questions_type'=>3])->count();
        
        return ['radio'=>$radio_num,'mulit'=>$mulit_num,'judge'=>$judge_num,'discuz'=>$discuz_num];
    }
    
    /**
     * store.
     *
     * @param array $input
     */
    public function store($input)
    {
        return $this->savePost($this->model, $input);
    }
    
    /**
     * 更新
     * @param int $id
     * @param $input
     * @return \App\Models\AccountEvents
     */
    public function update($id, $input)
    {
        $model = $this->model->find($id);
    
        return $this->savePost($model, $input);
    }
    
    /**
     * save.
     *
     * @param Request $input 输入
     */
    public function savePost($question, $input)
    {
        try{
            DB::beginTransaction();
        
            $question->fill($input);
            $question->save();
            
         /*   $question->jobstype()->sync([$input['jobstypeId']]);
            
            $question->skills()->sync([$input['skillId']]);*/
            
            DB::commit();
        } catch (PDOException $e) {
            Log::info(['保存题目'=> $e->getMessage()]);
            DB::rollBack();
        }
        
    
        return $question;
    }
}